package system.student.funtion.exhibition;

import system.student.entity.Course;
import system.student.entity.StudentCourseDetail;
import system.student.entity.login.LoginResponse;
import system.student.entity.signin.SignInRecord;
import system.student.entity.signin.SignInTask;
import system.student.entity.user.Student;
import system.student.entity.user.UserInfo;
import system.student.funtion.exhibition.studentfunction.ChoseCourseService;
import system.student.funtion.exhibition.studentfunction.impl.ChoseCourseServiceImpl;
import system.student.funtion.exhibition.teacherfunction.SignInService;
import system.student.funtion.exhibition.teacherfunction.impl.SignInServiceImpl;

import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class StudentExhibition {
    private static final SignInService signInService = new SignInServiceImpl();
    private static final ChoseCourseService choseCourseService = new ChoseCourseServiceImpl();

    public static void runningStudentExhibition(LoginResponse loginResponse) {
        Student user = (Student) loginResponse.getUser();

        while (true) {
            System.out.println(user.getName() + ": 欢迎来到学生页面");
            System.out.println("1.查看我的所有课程信息");
            System.out.println("2.根据课程号查看老师");
            System.out.println("3.根据课程名查看分数");
            System.out.println("4.选课");
            System.out.println("5.退课");
            System.out.println("6.签到");
            System.out.println("7.我的信息");
            System.out.println("8.返回");
            Scanner scanner = new Scanner(System.in);
            int option = scanner.nextInt();
            outerSwitch:
            switch (option) {
                case 1: {
                    List<StudentCourseDetail> courseList = choseCourseService.findAllCourseByStudentId(user.getId());

                    if (!courseList.isEmpty()) {
                        // 打印表头
                        System.out.println("+-----+------------+------------+-------+----------------+--------------------+");
                        System.out.println("|  ID | STUDENT_ID | COURSE_ID  | SCORE | TEACHER_NAME   | COURSE_NAME        |");
                        System.out.println("+-----+------------+------------+-------+----------------+--------------------+");

                        // 打印每条记录
                        for (StudentCourseDetail detail : courseList) {
                            System.out.println(detail);
                        }

                        // 打印表尾
                        System.out.println("+-----+------------+------------+-------+----------------+--------------------+");
                    } else {
                        System.err.println("您还没有选课");
                    }
                    break;
                }
                case 2: {
                    System.out.println("请输入课程号");
                    Long courseId = scanner.nextLong();

                    String name = choseCourseService.findById(courseId);
                    if (name != null) {
                        System.out.println("这门课老师是:" + name);
                    } else {
                        System.out.println("没有此课程");
                    }

                }
                break;
                case 3: {
                    System.out.println("请输入课程名");
                    scanner.nextLine();
                    String courseName = scanner.nextLine();
                    Integer score = choseCourseService.findScoreByCourseName(courseName, user.getId());
                    if (score != null) {
                        System.out.println("这门课的分数是:" + score);
                    } else {
                        System.out.println("没有此课程 或 尚未录入成绩");
                    }

                }
                break;
                case 4:
                    System.out.println("所有课程:");
                    List<Course> courseList = choseCourseService.selectAllCourse();
                    //获取所有课程id
                    List<Long> courseIdList = courseList.stream().map(Course::getId).collect(Collectors.toList());
                    if (courseIdList.isEmpty()) {
                        System.out.println("没有课程暂时无法选课");
                        break;
                    }
                    System.out.println("+------+----------------------+------+----------------+");
                    System.out.printf("| %-4s | %-20s | %-4s | %-14s |%n", "ID", "教师ID", "授课教师", "课程名字");
                    System.out.println("+------+----------------------+------+----------------+");

                    courseList.forEach(course -> {
                        System.out.printf("| %-4d | %-20s | %-4s | %-14s |%n",
                                course.getId(),
                                course.getTeacherId(),
                                course.getTeacherName(),
                                course.getCourseName());
                    });
                    System.out.println("+------+----------------------+------+----------------+");
                    System.out.println("请输入课程id");
                    Long courseId = scanner.nextLong();

                    if (choseCourseService.findIfHaveChosen(user.getId(), courseId)) {
                        System.out.println("您已经选过这门课了");
                        break;
                    }
                    for (Long id : courseIdList) {
                        if (id.equals(courseId)) {
                            choseCourseService.choseCourse(courseId, user.getId(),user.getName());
                            System.out.println("选课成功");
                            break outerSwitch;
                        }
                    }
                    System.out.println("请输入正确课程id");
                    break;
                case 5:{
                    while (true) {
                        System.out.println("请输入你要退课程的id:");
                        System.out.println("(取消退课操作请按0)");
                        Long CourseId = scanner.nextLong();
                        if(CourseId == 0){
                            System.out.println("取消退课");
                            break;
                        }
                        else if (!choseCourseService.findIfHaveChosen(user.getId(), CourseId)) {
                            System.out.println("您没有选过此课程");
                        }
                        else{
                            choseCourseService.concelCourse(CourseId, user.getId());
                            System.out.println("退课成功");
                            break;
                        }

                    }
                    break;
                }
                case 6: {
                    Long studentId = user.getUserId();
                    SignInTask signInTask =
                            signInService.selectTaskByStudentId(studentId);
                    if (signInTask != null) {
                        Boolean flag = signInTask.signIn(studentId);
                        if (flag) {
                            SignInRecord signInRecord =
                                    new SignInRecord(signInTask.getId(), user.getId(), user.getName());
                            signInService.insertSignInRecord(signInRecord);
                        }
                    } else {
                        System.out.println("暂无信息");
                    }
                }
                break;
                case 7: {
                    System.out.println("我的信息:");
                    String name = user.getName();
                    int age = user.getAge();
                    String gender = user.getGender();
                    UserInfo userInfo = user.getUserInfo();
                    System.out.println("username:" + userInfo.getUsername());
                    System.out.println("password:" + userInfo.getPassword());
                    System.out.println("phone:" + userInfo.getPhone());
                    System.out.println("name:" + name);
                    System.out.println("age:" + age);
                    System.out.println("gender:" + gender);
                }
                break;
                case 8: {
                    System.out.println("退出登录");
                    return;
                }
                default:
                    break;
            }
        }
    }
}
